<?php
class Filter {
	private $message = "";
	private $cp;
	private $au;
	function Filter() {

	}

	public function run() {
		$p1 = '/^@addfilter[\s]+(-d)\s+([0-9]{1,8})/';
		$p2 = '/^@addfilter[\s]+(.*)\s+([0-9]{1,3})/';
		preg_match($p1, trim($_POST[Message]), $m);
		preg_match($p2, trim($_POST[Message]), $n);

		if ($n[1] && $n[2] && trim($n[1])!="-d") {
			$this->addFilter($n);
		} else
			if ($m[1] == "-d" && $m[2]) {
				$this->delFilter($m);
			} else {
				$this->showFilter();
			}

	}

	private function addFilter($m) {
		$word = trim($m[1]);
		$arr = array (
			'keyword' => $word,
			'clusternum' => $_POST[ClusterNum],
			'sender' => $_POST[Sender],
			'say_count' => $m[2]
		);
		$ret = DB :: GetQueryResult("select * from td_filter_config where keyword='{$word}' and clusternum='{$_POST[ClusterNum]}'", true); //配置参数初始化
		if ($ret) {
			$mess = "关键字“{$word}”已经设置过了";
			$this->message = $mess;
		} else {
			DB :: SaveTableRow('td_filter_config', $arr);
		}
		$this->message = "设置成功";
	}

	private function delFilter($m) {
		$ret = DB :: GetQueryResult("select * from td_filter_config where id={$m[2]} and clusternum='{$_POST[ClusterNum]}'", true); //配置参数初始化
		if ($ret) {
			@ DB :: Query("delete from  td_filter_config  where id={$m[2]} and clusternum='{$_POST[ClusterNum]}'");
			//kill_session();
			$this->message = "删除成功~！";
		} else {
			$this->message = "不存在的规则 或者 规则非本群的~！";
		}

	}

	private function showFilter() {

		$ret = DB :: GetQueryResult("select * from td_filter_config where  clusternum='{$_POST[ClusterNum]}'", false); //配置参数初始化
		if ($ret) {
			$mess = "欢迎来到规则过滤中心\nid      限制次数          添加人qq           规则\n";
			foreach ($ret as $v) {
				$mess = $mess . "{$v[id]}  \t {$v[say_count]}\t\t{$v[sender]}\t规则：{$v[keyword]}\n";
			}
		} else {

			$mess = "本群暂时还没配置过滤规则。请配合白名单使用该功能。。";
		}
		$this->message = $mess . "\n使用说明如下：\n" .
		"1.添加一条规则：@addfilter  规则  次数\n" .
		"2.删除一条规则：@addfilter -d  id号" .
		"\n例子：如果本群群主不允许成员在群内发送诸如：\n“群主是个大笨蛋”," .
		"“群主就是个笨蛋”或者 “群主是笨蛋”等等类似语句。\n一旦触发规则立即给予警告" .
		"并且警告4次后T出本群。\n" .
		"此功能的配置如下：\n" .
		"@addfilter  群主.*是.*笨蛋    4\n" .
		"其实就是配一条正则，\n知道就弄，不会的千万不要使用filter功能\n不许带图片,后果自负";
	}

	public function filterCheck() {
		$ret1 = DB :: GetQueryResult("select * from td_filter_config where clusternum='{$_POST[ClusterNum]}'", false); //配置参数初始化
		foreach ($ret1 as $key => $s) {
			if (@ preg_match('/' . $s['keyword'] . '/is', $_POST[Message], $m)) {
				$sql = "select * from td_filter_message where  qq='{$_POST[Sender]}' and clusternum='{$_POST[ClusterNum]}' and filter_id='{$s[id]}'";
				$ret = DB :: GetQueryResult($sql, true); //配置参数初始化
				if ($ret) {
					if (($ret[say_num] + 1) >= $s[say_count]) {
						$mess =  preg_replace(array (
							"/\{0\}/",
							"/\{1\}/",
							"/\{2\}/"
						), array (
							$_POST[Nick],
							$m[0],
							$s[say_count]
						), $this->cp->getParamValue('FILTER_WORD_T_MESS')) . "\n5秒钟后执行T人";
						$api = new Api("SendClusterMessage");
						$this->message = $api->doApi($_POST[ClusterNum],$mess);
						sleep(5);
						require_once (ROOT_PATH . 'Lib/Mode/KickOutMember.class.php');
						$t = new KickOutMember();
						if( $t->kickOutM($_POST[Sender])) {
							$this->message = "踢出成功-。-";
						}else {
							$this->message = "踢出失败，原因：\n".$t->message;
						}
						$arr = array (
							'qq' => $_POST[Sender],
							'ClusterNum' => $_POST[ClusterNum],
							'operate' => "1004634672",
							'flag' => 0,
							'day' => date("Y-m-d H:i:s", time() + 8 * 3600)
						);
						@DB :: SaveTableRow('td_REMOVED_recode', $arr);
						return;
					} else {
						$n = $ret[say_num] + 1;
						$sql = "update   td_filter_message  set say_num={$n} where  qq='{$_POST[Sender]}' and clusternum='{$_POST[ClusterNum]}' and filter_id='{$s[id]}'";
						DB :: Query($sql);
						$this->message = preg_replace(array (
							"/\{0\}/",
							"/\{1\}/",
							"/\{2\}/",
							"/\{3\}/"
						), array (
							$_POST[Nick],
							$m[0],
							$n,
							$s[say_count]
						), $this->cp->getParamValue('FILTER_WORD_WARNING_MESS'));
					}
					return;
				} else {

					$arr = array (
						'filter_id' => $s[id],
						'qq' => $_POST[Sender],
						'clusternum' => $_POST[ClusterNum],
						'say_num' => 1
					);
					DB :: SaveTableRow('td_filter_message', $arr);
					$this->message = preg_replace(array (
						"/\{0\}/",
						"/\{1\}/",
						"/\{2\}/",
						"/\{3\}/"
					), array (
						$_POST[Nick],
						$m[0],
						"1",
						$s[say_count]
					), $this->cp->getParamValue('FILTER_WORD_WARNING_MESS'));

				}
				return;

			}
		}

	}

	private function __get($property_name) {
		return $this-> $property_name;

	}

	private function __set($property_name, $value) {
		$this-> $property_name = $value;
	}
}
?>